package com.kkb.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/pages/index.html")
public class LoginFilter implements Filter {

    /*必须需要继承Filter接口的init|doFilter|destroy三个方法，不然Tomcat跑不起来*/
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        //取存储的session
        Object adminInfo = httpServletRequest.getSession().getAttribute("adminInfo");

        if (adminInfo !=null ){
            //请求通过，可以访问index.html页面
            chain.doFilter(request,response);
        }else {
            //没有登录过，不允许登录index.html页面
            //将页面重定向到登录页面
            httpServletResponse.sendRedirect("/pages/login.html");
        }

    }

//火箭
    @Override
    public void destroy() {

    }
}
